package com.core.common;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class ExcelImpKit {
    /**
     * 取得单元格的值
     *
     * @param value
     * @return
     */
    public static String cell(String value) {
        if (value == null) {
            return "";
        }
        String r = value;
        r = r.replace("\r", "");
        r = r.replace("\n", "");
        return r.trim();
    }

    /**
     * 替换特殊字符
     *
     * @return
     */
    public static String replaceSpecial(String s) {
        if (s == null || s.length() == 0) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.toCharArray()[i];
            switch (c) {
                case ',':
                    sb.append("，");
                    break;
                case '<':
                    sb.append("《");
                    break;
                case '>':
                    sb.append("》");
                    break;
                case '[':
                    sb.append("【");
                    break;
                case ']':
                    sb.append("】");
                    break;
                case '(':
                    sb.append("（");
                    break;
                case ')':
                    sb.append("）");
                    break;
                case '\'':
                    sb.append("’");
                case '\"':
                    sb.append("’");
                    break;
                case '\\':
                    sb.append(" ");
                    break;
                case '/':
                    sb.append(" ");
                    break;
                case '\b':
                    sb.append(" ");
                    break;
                case '\f':
                    sb.append(" ");
                    break;
                case '\n':
                    sb.append(" ");
                    break;
                case '\r':
                    sb.append(" ");
                    break;
                case '\t':
                    sb.append(" ");
                    break;
                default:
                    ////在ASCⅡ码中，第0～31号及第127号(共33个)是控制字符或通讯专用字符
                    if ((c >= 0 && c <= 31) || c == 127) {
                        break;
                    } else {
                        sb.append(c);
                    }
                    break;
            }
        }
        return sb.toString();
    }

    public static Date parseDate(String inDate) {
        if (inDate == null || inDate.trim().length() == 0) {
            return null;
        }
        String date = inDate.trim();
        //数字
        if (date.chars().allMatch(Character::isDigit)) {
            try {
                int intDay = Integer.parseInt(date);
                Calendar calendar = new GregorianCalendar(1900, 0, -1);
                Date resDate = DateUtils.addDays(calendar.getTime(), intDay);
                return resDate;
            } catch (Exception ex) {
            }
        }
        if (date.endsWith("日")) {
            date = date.substring(0, date.length() - 1);
        }
        date = date.replace("/", "-");
        date = date.replace("年", "-");
        date = date.replace("月", "-");
        String[] ds = date.split("-");
        if (ds.length != 3) {
            return null;
        }
        String year = ds[0].trim();
        if (year.length() == 2) {
            year = "20" + year;
        }
        String month = ds[1].trim();
        if (month.length() == 1) {
            month = "0" + month;
        }
        String day = ds[2].trim();
        if (day.length() == 1) {
            day = "0" + day;
        }
        return DateUtil.parseDate(year + "-" + month + "-" + day);
    }
}
